﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

public class AttackTarget : MonoBehaviour, IPointerClickHandler
{
    public bool attackable;
    private CardDisplay display;

    private CharacterCard characterCard;
    // Start is called before the first frame update
    public void SetCharacterCard(CharacterCard characterCard)
    {

        if (characterCard == null)
        {
            Debug.LogError("Attempted to set a null CharacterCard!");
            return;
        }
        this.characterCard = characterCard;
        CardDisplay cardDisplay = GetComponent<CardDisplay>();
        if (cardDisplay == null)
        {
            Debug.LogError("CardDisplay component is missing on this GameObject!");
            return;
        }
        cardDisplay.card = characterCard;
        cardDisplay.ShowCard(); // 更新显示
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        if (attackable)
        {
            if (GetComponent<CardDisplay>().card != null)
            {
                //gameObject为攻击对象
                BattleManager.Instance.AttackConfirm(gameObject);
            }
            else
            {
                Debug.LogWarning("目标没有挂载 CardDisplay 组件，无法进行攻击！");
            }
        }
    }
    public void ApplyDamage(int _damage)
    {
        if (characterCard.shield <= 0)
        {
            characterCard.healthPoint -= _damage;
        }
        //之后游戏结束的判断加在这里
        if (characterCard.shield > 0)
        {
            characterCard.shield -= _damage;
            return;
        }
        if (characterCard.healthPoint <= 0)
        {
            gameObject.SetActive(false);
            SceneManager.LoadScene("DeathScene");
        }

    }
    public void ApplyShield(int _shield)
    {
        if (characterCard == null)
        {
            return;
        }
        characterCard.shield += _shield;
    }

    public void ApplyRegainhealth(int _effectvalue)
    {
        //Debug.Log(characterCard.healthPoint);
        characterCard.healthPoint += _effectvalue;
    }

    public void ApplyRegainmana(int _effectvalue)
    {
        characterCard.mana += _effectvalue;
        //Debug.Log(_effectvalue);
    }
    void Start()
    {
        display = GetComponent<CardDisplay>();
    }

    // Update is called once per frame
    void Update()
    {

    }
}
